#include "oiseau.h"

void initOiseau (Oiseau& oiseau)
{
  animInit( oiseau.anim, "data/anim2.ani" );
  oiseau.tex_tete = LoadGLTexture("data/terrain/venus.ppm", false);
  oiseau.tex_corp = LoadGLTexture("data/terrain/mars.ppm", false);
  oiseau.tex_aile = LoadGLTexture("data/terrain/mars.ppm", false);
}

void draw_oiseau (Oiseau& oiseau, const int v)
{
  float t = v*temps();
  int te = ((int) (t)) % 90;
  glTranslatef(0,1*cos(2*temps()),0);
  //corp
  glPushMatrix();
  glScalef(2,0.5,0.5);
  draw_sphere(oiseau.tex_corp);
  glPopMatrix();

  //tete
  glPushMatrix();
  glScalef(0.8,0.6,0.5);
  glTranslatef(-1.5,0,0);
  draw_sphere(oiseau.tex_tete);
  glPopMatrix();
  
  //bec
  glPushMatrix();
  glScalef(0.7,0.2,0.3);
  glTranslatef(-2.6,0,0);
  glRotatef(90,0,0,1);
  draw_cone();
  glPopMatrix();

  //queue
  glPushMatrix();
  glScalef(0.7,0.05,0.7);
  glTranslatef(3.5,0,0);
  glRotatef(90,0,0,1);
  draw_cone();
  glPopMatrix();
  
  //aile gauche
  glPushMatrix();
  glRotatef(te,1,0,0);
  glPushMatrix();
  glRotatef(45,1,0,0);
  glScalef(1,2,0.1);
  draw_cone();
  glPopMatrix();
  glPopMatrix();

  //aile droite
  glPushMatrix();
  glRotatef(te,-1,0,0);
  glPushMatrix();
  glRotatef(-45,1,0,0);
  glScalef(1,2,0.1);
  draw_cone();
  glPopMatrix();
  glPopMatrix();
}

void draw_migration(Oiseau& oiseau)
{
  glPushMatrix();  

  float t = 5*temps();
  int te = ((int) (t)) % oiseau.anim.nbp;
  float c = t - ((int) (t) );
  int te_suivant = (te+1) % oiseau.anim.nbp;
  int te_resuivant = (te_suivant + 1) % oiseau.anim.nbp;
  Vec3f pt_Pos,pt_Pos2, pt_Y;
  Vec3f vt_codir, vt_dirf;
 
  //premier point
  pt_Pos.x = (1 - c) * oiseau.anim.P[te].x + c * oiseau.anim.P[te_suivant].x;
  pt_Pos.y = (1 - c) * oiseau.anim.P[te].y + c * oiseau.anim.P[te_suivant].y;
  pt_Pos.z = (1 - c) * oiseau.anim.P[te].z + c * oiseau.anim.P[te_suivant].z;
  glTranslatef(pt_Pos.x, pt_Pos.y, pt_Pos.z);

  
  //second point
  pt_Pos2.x = (1 - c) * oiseau.anim.P[te_suivant].x + c * oiseau.anim.P[te_resuivant].x;
  pt_Pos2.y = (1 - c) * oiseau.anim.P[te_suivant].y + c * oiseau.anim.P[te_resuivant].y;
  pt_Pos2.z = (1 - c) * oiseau.anim.P[te_suivant].z + c * oiseau.anim.P[te_resuivant].z;
  glTranslatef(pt_Pos2.x, pt_Pos2.y, pt_Pos2.z);

  
  vecSub(vt_dirf,pt_Pos2, pt_Pos);
  vecNormalize(vt_dirf);
  
  vecInit(pt_Y,0,1,0);
  vecCross(vt_codir,vt_dirf,pt_Y);
  float m[16] = { vt_dirf.x, vt_dirf.y, vt_dirf.z, 0,
		  0    ,     1,     0, 0,
		  vt_codir.x,vt_codir.y,vt_codir.z,0,
		  0      ,      0,      0,1};
  glMultMatrixf(m);
  glPushMatrix();
    {
      glRotatef(180,0,1,0);
      glScalef(0.2,0.2,0.2);
      glTranslatef(0,1,0);
      glPushMatrix();
      draw_oiseau(oiseau, 280);
      glPopMatrix();
      glPushMatrix();
      glTranslatef(5,0,5);
      draw_oiseau(oiseau, 200);
      glPopMatrix();
      glPushMatrix();
      glTranslatef(5,0,-5);
      draw_oiseau(oiseau, 220);
      glPopMatrix();
      glPushMatrix();
      glTranslatef(10,0,10);
      draw_oiseau(oiseau, 290);
      glPopMatrix();
      glPushMatrix();
      glTranslatef(10,0,-10);
      draw_oiseau(oiseau, 200);
      glPopMatrix();
    }
    glPopMatrix();
    glPopMatrix();
}
